package com.pz.cloud.framework.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.cloud.framework.base.dao.CommonDao;
import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.business.material.entity.Material;
import com.pz.cloud.framework.business.material.service.IMaterialService;
import com.pz.cloud.framework.business.student.entity.Student;
import com.pz.cloud.framework.business.workflow.enums.WorkflowEnum;
import com.pz.cloud.framework.material.mapper.MaterialMapper;
import com.pz.cloud.framework.model.AbstractSqlBuilder;
import com.pz.cloud.framework.model.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {
    @Autowired
    private CommonDao commonDao;

    @Override
    public boolean save(Material entity) {
        String code = entity.getCode();
        if (StringUtils.isNotBlank(code) && getByCode(code) != null) {
            throw new CustomRuntimeException("该编号已存在");
        }
        return super.save(entity);
    }

    @Override
    public Material getByCode(String code) {
        LambdaQueryWrapper<Material> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Material::getCode, code);
        List<Material> list = list(wrapper);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public IPage<Material> findPage(Material material) {
        SqlBuilder sqlBuilder = SqlBuilder.init(material, null);
        sqlBuilder.filterDataScope("");
        return commonDao.findPage(sqlBuilder);
    }
}
